package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.StockAddCartService;
import com.mall.order.dto.StockAddCartResponse;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.AddCartRequest;
import com.mall.shopping.dto.AddCartResponse;
import com.mall.user.annotation.Anonymous;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import com.mall.user.constants.SysRetCodeConstants;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * create by heyyon 2020-12-20 20:00
 */
@RestController
@Slf4j
public class AddShoppingCartController {
    @Reference(check = false)
    ICartService iCartService;
    @Reference(check=false)
    StockAddCartService stockAddCartService;
    @PostMapping("shopping/carts")
    public ResponseData addToCart(@RequestBody Map<String, String> map) {
        Long userId=null;
        Long productId=null;
        Long productNum=null;
        try {
            String userIdStr = map.get("userId");
            userId = Long.parseLong(userIdStr);
            String productIdStr = map.get("productId");
            productId = Long.parseLong(productIdStr);
            String productNumStr = map.get("productNum");
            productNum = Long.parseLong(productNumStr);
        } catch (NumberFormatException e) {
            log.error("参数非法: "+e);
            return new ResponseUtil<>().setErrorMsg("参数非法");
        }
        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(userId);
        addCartRequest.setItemId(productId);
        addCartRequest.setNum(productNum);
        AddCartResponse addCartResponse=iCartService.addToCart(addCartRequest);
        if (!addCartResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(addCartResponse.getMsg());
        }
        return new ResponseUtil<>().setData(addCartResponse.getMsg());
    }
}
